{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pyvis\n",
      "  Downloading https://files.pythonhosted.org/packages/ab/4b/e37e4e5d5ee1179694917b445768bdbfb084f5a59ecd38089d3413d4c70f/pyvis-0.3.2-py3-none-any.whl (756kB)\n",
      "Requirement already satisfied: networkx>=1.11 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from pyvis) (2.1)\n",
      "Requirement already satisfied: ipython>=5.3.0 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from pyvis) (6.5.0)\n",
      "Collecting jsonpickle>=1.4.1 (from pyvis)\n",
      "  Downloading https://files.pythonhosted.org/packages/c0/a3/e610ae0feba3e7374da08ab6cc9bb76c8bfa84b4e502aa357bda0ef6dcae/jsonpickle-3.4.2-py3-none-any.whl (46kB)\n",
      "Requirement already satisfied: jinja2>=2.9.6 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from pyvis) (2.10)\n",
      "Requirement already satisfied: decorator>=4.1.0 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from networkx>=1.11->pyvis) (4.3.0)\n",
      "Requirement already satisfied: pickleshare in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (0.7.4)\n",
      "Requirement already satisfied: backcall in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (0.1.0)\n",
      "Requirement already satisfied: jedi>=0.10 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (0.12.1)\n",
      "Requirement already satisfied: pygments in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (2.2.0)\n",
      "Requirement already satisfied: simplegeneric>0.8 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (0.8.1)\n",
      "Requirement already satisfied: traitlets>=4.2 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (4.3.2)\n",
      "Requirement already satisfied: colorama; sys_platform == \"win32\" in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (0.3.9)\n",
      "Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.15 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (1.0.15)\n",
      "Requirement already satisfied: setuptools>=18.5 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from ipython>=5.3.0->pyvis) (40.2.0)\n",
      "Requirement already satisfied: MarkupSafe>=0.23 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from jinja2>=2.9.6->pyvis) (1.0)\n",
      "Requirement already satisfied: parso>=0.3.0 in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from jedi>=0.10->ipython>=5.3.0->pyvis) (0.3.1)\n",
      "Requirement already satisfied: six in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from traitlets>=4.2->ipython>=5.3.0->pyvis) (1.11.0)\n",
      "Requirement already satisfied: ipython-genutils in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from traitlets>=4.2->ipython>=5.3.0->pyvis) (0.2.0)\n",
      "Requirement already satisfied: wcwidth in c:\\users\\administrator\\anaconda3\\lib\\site-packages (from prompt-toolkit<2.0.0,>=1.0.15->ipython>=5.3.0->pyvis) (0.1.7)\n",
      "Installing collected packages: jsonpickle, pyvis\n",
      "Successfully installed jsonpickle-3.4.2 pyvis-0.3.2\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install pyvis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Collecting pyltp\n",
      "  Downloading https://files.pythonhosted.org/packages/f6/ee/d33a93ba1ddf10923b895d304cf100c39f404237bb4583505f2d902e6c45/pyltp-0.4.0-cp37-cp37m-win_amd64.whl (564kB)\n",
      "Installing collected packages: pyltp\n",
      "Successfully installed pyltp-0.4.0\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "twisted 18.7.0 requires PyHamcrest>=1.9.0, which is not installed.\n",
      "You are using pip version 10.0.1, however version 24.0 is available.\n",
      "You should consider upgrading via the 'python -m pip install --upgrade pip' command.\n"
     ]
    }
   ],
   "source": [
    "!pip install pyltp"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import re\n",
    "from pyvis.network import Network\n",
    "from  pyltp import Segmentor, Postagger, Parser, SementicRoleLabeller"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "def parser_main(sentence):\n",
    "    LTP_DIR=\"./ltp_data_v3.4.0\"\n",
    "    segmentor=Segmentor(os.path.join(LTP_DIR,\"cws.model\"))\n",
    "    postagger=Postagger(os.path.join(LTP_DIR,\"pos.model\"))\n",
    "    parser=Parser(os.path.join(LTP_DIR,\"parser.model\"))\n",
    "    labeller=SementicRoleLabeller(os.path.join(LTP_DIR,\"pisrl_win.model\"))\n",
    "    words=list(segmentor.segment(sentence))\n",
    "    postags=list(postagger.postag(words))\n",
    "    arcs=parser.parse(words,postags)\n",
    "    roles=labeller.label(words,postags,arcs)\n",
    "    roles_dict={key:{sub_key:[sub_key,*value] for sub_key,value in sub_data}for key,sub_data in roles}\n",
    "    return words,postags,arcs,roles_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分词结果: ['苏轼', '是', '宋朝', '的', '著名', '文学家', '，', '黄庭坚', '是', '苏轼', '的', '好', '朋友', '。', '苏轼', '擅长', '写', '词', '，', '而', '黄庭坚', '擅长', '写', '诗', '。', '黄庭坚', '游览', '黄州', '，', '并', '赞叹', '黄山', '之', '美', '。', '黄庭坚', '探望', '苏轼', '，', '并', '一起', '吟诗', '作', '对', '。']\n",
      "语义角色标注结果: {1: {'A0': ['A0', 0, 0], 'A1': ['A1', 2, 5]}, 8: {'A0': ['A0', 7, 7], 'A1': ['A1', 9, 12]}, 15: {'A0': ['A0', 14, 14]}, 16: {'A1': ['A1', 17, 17]}, 21: {'A0': ['A0', 20, 20], 'A1': ['A1', 23, 23]}, 22: {'A1': ['A1', 23, 23]}, 26: {'A0': ['A0', 25, 25], 'A1': ['A1', 27, 27]}, 30: {'DIS': ['DIS', 29, 29], 'A1': ['A1', 31, 33]}, 36: {'A0': ['A0', 35, 35], 'A1': ['A1', 37, 37]}, 41: {'A0': ['A0', 35, 35], 'DIS': ['DIS', 39, 39], 'ADV': ['ADV', 40, 40]}, 42: {'ADV': ['ADV', 40, 40]}}\n"
     ]
    }
   ],
   "source": [
    "text=\"苏轼是宋朝的著名文学家，黄庭坚是苏轼的好朋友。苏轼擅长写词，而黄庭坚擅长写诗。黄庭坚游览黄州，并赞叹黄山之美。黄庭坚探望苏轼，并一起吟诗作对。\"\n",
    "words,postags,arcs,roles_dict=parser_main(text)\n",
    "print(\"分词结果:\",words)\n",
    "print(\"语义角色标注结果:\",roles_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "def ruler(words,postags,roles_dict,role_index):\n",
    "    v=words[role_index]\n",
    "    role_info=roles_dict[role_index]\n",
    "    if 'A0'in role_info.keys()and 'A1'in role_info.keys():\n",
    "        s=''.join([words[word_index]for word_index in range(role_info['A0'][1], role_info['A0'][2]+1) if postags[word_index][0]not in['w','u','x']and words[word_index]])\n",
    "        o=''.join([words[word_index]for word_index in range(role_info['A1'][1], role_info['A1'][2]+1) if postags[word_index][0]not in['w','u','x']and words[word_index]])\n",
    "        if s and o:\n",
    "            return'1',[s,v,o]\n",
    "        return '4',[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re\n",
    "\n",
    "def triples_main(text):\n",
    "    sentences = [sentence for sentence in re.split(r'[？?！!。；;：:\\n\\r]', text) if sentence]\n",
    "    svos = []\n",
    "    for sentence in sentences:\n",
    "        words, postags, arcs, roles_dict = parser_main(sentence)\n",
    "        for idx in range(len(postags)):\n",
    "            if idx in roles_dict:\n",
    "                result = ruler(words, postags, roles_dict, idx)\n",
    "                if result and len(result) == 2:  \n",
    "                    flag, triple = result\n",
    "                    if flag == '1' and triple:  \n",
    "                        svos.append(triple)\n",
    "    return svos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "ename": "MemoryError",
     "evalue": "bad allocation",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mMemoryError\u001b[0m                               Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-20-3322bd8352ea>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0msvos\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mtriples_main\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"文本的三元组:svos={0}\"\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msvos\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-19-9a326bfd78e0>\u001b[0m in \u001b[0;36mtriples_main\u001b[1;34m(text)\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0msvos\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0msentence\u001b[0m \u001b[1;32min\u001b[0m \u001b[0msentences\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m         \u001b[0mwords\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mpostags\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0marcs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mroles_dict\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mparser_main\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msentence\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0midx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpostags\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0midx\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mroles_dict\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-10-e29b5e220d09>\u001b[0m in \u001b[0;36mparser_main\u001b[1;34m(sentence)\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mparser_main\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msentence\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m     \u001b[0mLTP_DIR\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m\"./ltp_data_v3.4.0\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m     \u001b[0msegmentor\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mSegmentor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mLTP_DIR\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"cws.model\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mpostagger\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mPostagger\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mLTP_DIR\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"pos.model\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[0mparser\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mParser\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mos\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mLTP_DIR\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"parser.model\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mMemoryError\u001b[0m: bad allocation"
     ]
    }
   ],
   "source": [
    "svos=triples_main(text)\n",
    "print(\"文本的三元组:svos={0}\".format(svos))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_node(net,subs1,subs2):\n",
    "    if len(subs1) !=0:\n",
    "        for i in range(len(subs1)):\n",
    "            net.add_node(i,label=subs[i],color=\"blue\")\n",
    "    if len(subs2) !=0:\n",
    "        for i in range(len(subs2)):\n",
    "            net.add_node(1000+i,label=subs2[i],color=\"green\")\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_node_id_dic(net):\n",
    "    dic_node_id={}\n",
    "    for i in net,node_ids:\n",
    "        dic_node_id[str(net.node+map[i][\"label\"])]=i\n",
    "    return dic_node_id"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_network(net,kg_list,node_id_dic):\n",
    "    for m in range(len(kg_list)):\n",
    "        try:\n",
    "            net.add_edge(node_id_dic[kg_list[m][0]],node_id_dic[kg_list[m][2],label=kg_list[m][1],color=\"red\",width=2])\n",
    "            except AttributeError as e:\n",
    "                print(e,m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "net=Network(notebook=True,directed=True)\n",
    "subs1=list(set(sublist[0] for sublist in svos))\n",
    "subs2=list(set(sublist[2] for sublist in svos))\n",
    "create_node(net,subs1,subs2)\n",
    "dic_node_id=create_node_id_dic(net)\n",
    "create_network(net,svos,dic_node_id)\n",
    "net.show(\"my_network.html\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_similar_semantics(net,node_id_dic,\n",
    "                          target_node):\n",
    "    target_node_id=node_id_dic[target_node]\n",
    "    adiacent_nodes=net.neighbors(target_node_id)\n",
    "    similar_semantics = [node_id_dic_inverse[node_id] for node_id in adjacent_nodes]\n",
    "    return similar_semantics"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "node_id_dic_inverse={v:k for k,v in dic_node_id.items()}\n",
    "similar=find_similar_semantics(net,dic_node_id,'苏俄')\n",
    "print(\"与目标词'苏轼'语义相关的词:\",similar)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
